package com.shiyanlou.upms.system.service.impl;


import com.shiyanlou.upms.system.dao.UpmsPermissionMapper;
import com.shiyanlou.upms.system.dao.UpmsRoleMapper;
import com.shiyanlou.upms.system.dao.UpmsRolePermissionMapper;
import com.shiyanlou.upms.system.dao.UpmsUserRoleMapper;
import com.shiyanlou.upms.system.domain.UpmsPermission;
import com.shiyanlou.upms.system.domain.UpmsRole;
import com.shiyanlou.upms.system.domain.UpmsRolePermission;
import com.shiyanlou.upms.system.domain.UpmsUserRole;
import com.shiyanlou.upms.system.service.UpmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("upmsRoleService")
public class UpmsRoleServiceImpl implements UpmsRoleService {

    @Autowired
    private UpmsRoleMapper upmsRoleMapper;

    @Autowired
    private UpmsPermissionMapper upmsPermissionMapper;

    @Autowired
    private UpmsRolePermissionMapper upmsRolePermissionMapper;

    @Autowired
    private UpmsUserRoleMapper upmsUserRoleMapper;

    @Override
    public int deleteByPrimaryKey(Integer roleId) {
        return upmsRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public int insert(UpmsRole record) {
        return upmsRoleMapper.insert(record);
    }

    @Override
    public int insertSelective(UpmsRole record) {
        return upmsRoleMapper.insertSelective(record);
    }

    @Override
    public UpmsRole selectByPrimaryKey(Integer roleId) {
        return upmsRoleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public int updateByPrimaryKeySelective(UpmsRole record) {
        return upmsRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UpmsRole record) {
        return upmsRoleMapper.updateByPrimaryKey(record);
    }

    @Override
    public int count(Map<String, Object> map) {
        return upmsRoleMapper.count(map);
    }

    @Override
    public List<UpmsRole> list(Map<String, Object> map) {
        return upmsRoleMapper.list(map);
    }

    @Override
    public int batchRemove(Integer[] userIds) {
        return upmsRoleMapper.batchRemove(userIds);
    }

    /**
     * 获取角色拥有的资源
     * 将所有资源树加载出来，再将用户拥有的资源加载出来，通过遍历将资源树打上标记。
     */
    @Override
    public List<UpmsPermission> selectRoleHasPermissionByRoleId(Long roleId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("roleId", roleId);
        List<UpmsRolePermission> rolePermissionDOList = upmsRolePermissionMapper.list(paramMap);
        //获取所有的资源树
        List<UpmsPermission> resultList = upmsPermissionMapper.list(new HashMap<String, Object>());
        for (UpmsPermission result : resultList) {
            for (UpmsRolePermission rolePermissionDO : rolePermissionDOList) {
                result.setChecked(false);
                if (result.getPermissionId().equals(rolePermissionDO.getPermissionId())) {
                    result.setChecked(true);
                    break;
                }
            }
        }
        return resultList;
    }
    /**
     * 更新角色信息
     * @param upmsRole
     * @param permissionArr
     * @return
     */
    @Transactional
    @Override
    public int update(UpmsRole upmsRole, Integer[] permissionArr) {
        List<UpmsRolePermission> upmsRolePermissionList = new ArrayList<UpmsRolePermission>();
        int count = updateByPrimaryKeySelective(upmsRole);
        upmsRolePermissionMapper.deleteRolePermissionByRoleId(upmsRole.getRoleId());
        for (Integer permission : permissionArr) {
            UpmsRolePermission upmsRolePermissionDO = new UpmsRolePermission();
            upmsRolePermissionDO.setRoleId(upmsRole.getRoleId());
            upmsRolePermissionDO.setPermissionId(permission);
            upmsRolePermissionList.add(upmsRolePermissionDO);
        }
        upmsRolePermissionMapper.batchInsert(upmsRolePermissionList);
        return count;
    }

    @Override
    public List<UpmsRole> selectRoleByUserId(Integer userId) {
        return upmsRoleMapper.selectRoleByUserId(userId);
    }

    @Override
    public int selectRoleCountByUserId(Integer userId) {
        return upmsRoleMapper.selectRoleCountByUserId(userId);
    }

    @Override
    public int batchAddUser(Integer roleId, Integer[] userIds) {
        int count = 0;
        for (int i=0; i < userIds.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("userId", userIds[i]);
            //没有数据才更新
            if(upmsUserRoleMapper.count(map) == 0) {
                UpmsUserRole upmsUserRole = new UpmsUserRole();
                upmsUserRole.setRoleId(roleId);
                upmsUserRole.setUserId(userIds[i]);
                count += upmsUserRoleMapper.insertSelective(upmsUserRole);
            }
        }
        return count;
    }

    // 逐笔删除
    @Override
    public int deleteUserRoleInfo(Integer roleId, Integer userId) {
        return upmsUserRoleMapper.deleteByUserRole(roleId, userId);
    }

    // 批量删除
    @Transactional
    @Override
    public int batchRemoveRole(Integer roleId, Integer[] userIds) {
        int count =0;
        for (int i=0; i < userIds.length; i++) {
            count += upmsUserRoleMapper.deleteByUserRole(roleId, userIds[i]);
        }
        return count;
    }
}
